<?php

function Map2Table($csvFile, $TableName, $Rows, $Options) {
//
    if (strpos($csvFile, ".csv")) {
        $dlim = ",";
    } else {
        $dlim = "\t";
    }
    // get table columns
    //
    $TableCols = array();
    $SQL = "SELECT COLUMN_NAME FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_NAME`='$TableName'";
    $result = mysql_query($SQL);
    $num_rows = mysql_num_rows($result);
    if ($result) {
        if ($num_rows > 0) {
            while ($db_field = mysql_fetch_assoc($result)) {
                $TableCols[] = $db_field["COLUMN_NAME"];
            }
        }
    }
    $TblCount = count($TableCols);
    //
    //get csv columns
    //
    $file_handle = fopen($csvFile, "r");
    $line1 = fgets($file_handle);
    $csvCols = explode($dlim, $line1);
    $Sample = "";
    for ($row = 1; $row < $Rows; $row++) {
        if (!feof($file_handle)) {
            $line = fgets($file_handle);
            $Sample = explode($dlim, $line);
        }
    }
    $csvCount = count($csvCols);
    $ColCount = $csvCount;
    if ($ColCount < $TblCount) {
        $ColCount = $TblCount;
    }
    fclose($file_handle);
    //
    // build mapping form
    //
    $HTML = "<br><table align=\"center\" cellspacing=\"1\"cellpadding=\"0\">";
    //$HTML = $HTML . "<tr><td class=\"chooser\"><br>Map: <b> $csvFile </b>to: <b> $TableName </b><br></td></tr>";
    $HTML = $HTML . "<tr><td><form name=\"csvIn\" method=\"post\" action=\"ImportMapUpdate.php\">";
    $HTML = $HTML . "<input name=\"csvFile\" type=\"hidden\" value=\"" . $csvFile . "\">";
    $HTML = $HTML . "<input name=\"TableName\" type=\"hidden\" value=\"" . $TableName . "\">";
    $HTML = $HTML . "<input name=\"csvCount\" type=\"hidden\" value=\"" . $csvCount . "\">";
    $HTML = $HTML . "<table ><tr><th> " . $csvFile . "</th>";
    if ($Rows > 1) {
        $HTML = $HTML . "<th> Sample Data</th>";
    } else {
        $HTML = $HTML . "<th> </th>";
    }
    $HTML = $HTML . "<th>Map To &gt;</th><th></th><th>" . $TableName . " Table</th><th>User Exit</th></tr>";
    //$HTML = $HTML . "</tr>";
    for ($i = 0; $i < $ColCount; $i++) {
        $HTML = $HTML . "<tr>";
        if ($i < $csvCount) {
            $HTML = $HTML . "<td class=\"chooser\">" . $csvCols[$i] . "</td><td>" . $Sample[$i] . "</td>";
            $HTML = $HTML . "<td><input name=\"csvCol[" . $i . "]\" type=\"hidden\" id=\"csvCol\" value=\"" . $csvCols[$i] . "\"/>";
            $HTML = $HTML . "<input name=\"MapTo[" . $i . "]\" size=\"2\" maxlength=\"2\" type=\"text\" id=\"MapTo\"/></td>";
        } else {
            $HTML = $HTML . "<td></td><td></td><td></td>";
        }
        $HTML = $HTML . "<td class=\"chooser\">" . $i . "</td><td class=\"chooser\">" . $TableCols[$i];
        //if ( $Chsr > 0 ) {
        $HTML = $HTML . "<input name=\"TableCol[" . $i . "]\" type=\"hidden\" id=\"TableCol\" value=\"" . $TableCols[$i] . "\"/></td>";
        $HTML = $HTML . "<td><input name=\"UserExit[" . $i . "]\" size=\"20\" maxlength=\"20\" type=\"text\" id=\"UserExit\"/></td>";
        $HTML = $HTML . "</tr>";
    }
    $HTML = $HTML . "</table>";
    $HTML = $HTML . "<br><input id=\"submit\" name=\"submit\" type=\"submit\" value=\"Submit\" onClick=\"return(valid(document.frmclaim.txttele));\" />";
    $HTML = $HTML . "</form></td></tr></table>";

    return $HTML;
}

?>
